const path = require("path");
const webpack = require("webpack");
const CopyWebpackPlugin = require("copy-webpack-plugin");

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  configureWebpack: {
    name: "vue-cesium",
    resolve: {
      alias: {
        "@": resolve("src"),
      },
    },
    plugins: [
      new CopyWebpackPlugin([
        {
          from: "node_modules/cesium/Build/Cesium/Workers",
          to: "cesium/Workers",
        },
        {
          from: "node_modules/cesium/Build/Cesium/ThirdParty",
          to: "cesium/ThirdParty",
        },
        {
          from: "node_modules/cesium/Build/Cesium/Assets",
          to: "cesium/Assets",
        },
        {
          from: "node_modules/cesium/Build/Cesium/Widgets",
          to: "cesium/Widgets",
        },
      ]),
      new webpack.DefinePlugin({
        CESIUM_BASE_URL: JSON.stringify("./cesium"),
      }),
    ],
    module: {
      rules: [
        {
          test: /.js$/,
          include: [resolve("node_modules/cesium/Source")],
          use: {
            loader: require.resolve("@open-wc/webpack-import-meta-loader"),
          },
        },
        {
          test: /.js$/,
          include: [resolve("node_modules/@cesium/engine/Source")],
          use: "babel-loader",
        },
        {
          test: /\.(gltf)$/,
          use: {
            loader: "url-loader"
          },
        },
      ],
      unknownContextCritical: false,
    },
  },
};
